﻿@model Bricks.Site.Models.ContractFilterView
@{
    ViewBag.Title = "YearPlanList";
    Layout = "~/Views/Shared/_UserPage.cshtml";
}


<script type="text/javascript">
    $(function () {
        var userType = $("#hUserType").val();
        if (userType == "0") {
            var trCommom = document.getElementById('trCommon');
            var table = trCommom.parentNode;
            table.removeChild(trCommom);

            var url = '/YearPlan/GetYearPlanList?projectID=' + $("#hProjectID").val() + '&contractType=' + encodeURI($("#ContractType").val()) + '&contractID=' + $("#hContractID").val();
            initTable(url);
        }
        else {
            var trRequest = document.getElementById('trRequest');
            var table = trRequest.parentNode;
            table.removeChild(trRequest);

            $('#ContractType').combobox({
                editable: false, panelHeight: "auto",
                valueField: "DictionaryName",
                textField: "DictionaryName",
                url: "/Dictionary/GetDictionaryByTypeTag?typeTag=ContractType",
                onLoadSuccess: function () {
                    var data = $('#ContractType').combobox('getData');
                    if (data.length > 0) {
                        $('#ContractType').combobox('select', data[0]["DictionaryName"]);
                    }
                },
                onSelect: function (record) {
                    var projectID = $("#ProjectID").combobox('getValue');
                    initContract(projectID, record.DictionaryName);
                }
            });

            $("#ProjectID").combobox({
                editable: false, panelHeight: "auto",
                valueField: 'ID',
                textField: 'ProjectName',
                url: '/ProjectInfo/ProjectInfoListForFlow',
                onLoadSuccess: function () {
                    var data = $("#ProjectID").combobox('getData');
                    if (data.length > 0) {
                        $("#ProjectID").combobox('select', data[0]["ID"]);
                    }
                },
                onSelect: function (record) {
                    var contractType = $("#ContractType").combobox('getValue');
                    initContract(record.ID, contractType);
                }
            });
        }
    });

    function initContract(projectID, contractType) {
        $("#ContractID").combobox({
            editable: false, panelHeight: "auto", multiple: true, separator: ',',
            valueField: 'ID',
            textField: 'ContractName',
            url: '/ContractInfo/ContractInfoListByProjectIDAndType?projectID=' + projectID + '&contractType=' + encodeURI(contractType),
            onLoadSuccess: function () {
                //var contractIDArr = new Array();
                var data = $("#ContractID").combobox('getData');
                if (data.length > 0) {
                    //for (var i = 0; i < data.length; i++) {
                    //    contractIDArr[i] = data[i]["ID"];
                    //}
                    $("#ContractID").combobox('select', data[0]["ID"]);
                }
                //$("#ContractID").combobox('setValues', contractIDArr);
            }
        });
    }

    function Search() {
        var projectID = $("#ProjectID").combobox('getValue');
        var contractType = $("#ContractType").combobox('getValue');
        var contractIDStr = $("#ContractID").combobox('getValues').toString();
        var url = '/YearPlan/GetYearPlanListForPreview?projectID=' + projectID + '&contractType=' + encodeURI(contractType) + '&contractIDStr=' + contractIDStr;
        initTable(url);
    }

    function initTable(dataUrl) {
        $('#yearPlanGrid').datagrid({
            url: dataUrl,
            iconCls: 'icon-bricks',
            nowrap: true,//当数据长度超出列宽时将会自动截取
            autoRowHeight: false,
            striped: true,//设置为true将交替显示行背景
            collapsible: true,
            pagination: true,
            pageSize: 20,
            remoteSort: false,
            idField: 'ID',//表明该列是一个唯一列。
            singleSelect: true,
            fitColumns: true,
            toolbar: '#yearPlanToolBar',
            columns:
            [
                [
                    { title: 'ID', field: 'ID', hidden: true },
                    { title: '统计周期', field: 'StatisticalCycle', align: 'center', rowspan: 2, width: 70 },
                    { title: '单位', field: 'Unit', align: 'center', rowspan: 2, width: 50 },
                    { title: '总金额', field: 'TotalMoney', align: 'center', rowspan: 2, width: 100 },
                    { title: '年度计划', field: '', colspan: 2, width: 150 },
                    { title: '1月份', field: '', colspan: 2, width: 170 },
                    { title: '2月份', field: '', colspan: 2, width: 170 },
                    { title: '3月份', field: '', colspan: 2, width: 170 },
                    { title: '4月份', field: '', colspan: 2, width: 170 },
                    { title: '5月份', field: '', colspan: 2, width: 170 },
                    { title: '6月份', field: '', colspan: 2, width: 170 },
                    { title: '7月份', field: '', colspan: 2, width: 170 },
                    { title: '8月份', field: '', colspan: 2, width: 170 },
                    { title: '9月份', field: '', colspan: 2, width: 170 },
                    { title: '10月份', field: '', colspan: 2, width: 170 },
                    { title: '11月份', field: '', colspan: 2, width: 170 },
                    { title: '12月份', field: '', colspan: 2, width: 170 }
                ],
                [
                     { title: '金额', field: 'YearPlanMoney', align: 'center', width: 90 },
                     { title: '占总金额%', field: 'YearPlanProportion', align: 'center', width: 60 },
                     { title: '金额', field: 'JanuaryMoneyNum', align: 'center', width: 90 },
                     { title: '占年度计划%', field: 'JanuaryProportion', align: 'center', width: 80 },
                     { title: '金额', field: 'FebruaryMoneyNum', align: 'center', width: 90 },
                     { title: '占年度计划%', field: 'FebruaryProportion', align: 'center', width: 80 },
                     { title: '金额', field: 'MarchMoneyNum', align: 'center', width: 90 },
                     { title: '占年度计划%', field: 'MarchProportion', align: 'center', width: 80 },
                     { title: '金额', field: 'AprilMoneyNum', align: 'center', width: 90 },
                     { title: '占年度计划%', field: 'AprilProportion', align: 'center', width: 80 },
                     { title: '金额', field: 'MayMoneyNum', align: 'center', width: 90 },
                     { title: '占年度计划%', field: 'MayProportion', align: 'center', width: 80 },
                     { title: '金额', field: 'JuneMoneyNum', align: 'center', width: 90 },
                     { title: '占年度计划%', field: 'JuneProportion', align: 'center', width: 80 },
                     { title: '金额', field: 'JulyMoneyNum', align: 'center', width: 90 },
                     { title: '占年度计划%', field: 'JulyProportion', align: 'center', width: 80 },
                     { title: '金额', field: 'AugustMoneyNum', align: 'center', width: 90 },
                     { title: '占年度计划%', field: 'AugustProportion', align: 'center', width: 80 },
                     { title: '金额', field: 'SeptemberMoneyNum', align: 'center', width: 90 },
                     { title: '占年度计划%', field: 'SeptemberProportion', align: 'center', width: 80 },
                     { title: '金额', field: 'OctoberMoneyNum', align: 'center', width: 90 },
                     { title: '占年度计划%', field: 'OctoberProportion', align: 'center', width: 80 },
                     { title: '金额', field: 'NovemberMoneyNum', align: 'center', width: 90 },
                     { title: '占年度计划%', field: 'NovemberProportion', align: 'center', width: 80 },
                     { title: '金额', field: 'DecemberMoneyNum', align: 'center', width: 90 },
                     { title: '占年度计划%', field: 'DecemberProportion', align: 'center', width: 80 }
                ]
            ],
            onClickRow: function (rowIndex, rowData) {
                $('#btnRevise').linkbutton('enable');
                $('#btnDelete').linkbutton('enable');
                var userType = $("#hUserType").val();
                if (userType == "0") {
                    initRevise(rowData.ID);
                }
                else {
                    var projectID = $("#ProjectID").combobox('getValue');
                    var contractType = $("#ContractType").combobox('getValue');
                    var contractIDStr = $("#ContractID").combobox('getValues').toString();
                    var url = '/YearPlan/GetYearPlanDetailListForPreview?projectID=' + projectID + '&contractType=' + encodeURI(contractType) + '&contractIDStr=' + contractIDStr + '&statisticalCycle=' + encodeURI(rowData.StatisticalCycle);
                    initDetailTable(url);
                }
            },
            onLoadSuccess: function (data) {
                if (data.rows.length > 0) {
                    $('#yearPlanGrid').datagrid('selectRow', 0);
                    var row = $("#yearPlanGrid").datagrid("getSelected");
                    $('#btnRevise').linkbutton('enable');
                    $('#btnDelete').linkbutton('enable');

                    var userType = $("#hUserType").val();
                    if (userType == "0") {
                        initRevise(row.ID);
                    }
                    else {
                        var projectID = $("#ProjectID").combobox('getValue');
                        var contractType = $("#ContractType").combobox('getValue');
                        var contractIDStr = $("#ContractID").combobox('getValues').toString();
                        var url = '/YearPlan/GetYearPlanDetailListForPreview?projectID=' + projectID + '&contractType=' + encodeURI(contractType) + '&contractIDStr=' + contractIDStr + '&statisticalCycle=' + encodeURI(row.StatisticalCycle);
                        initDetailTable(url);
                    }
                }
                else {
                    var userType = $("#hUserType").val();
                    if (userType == "0") {
                        initRevise('00000000-0000-0000-0000-000000000000');
                    }
                    else {
                        var projectID = $("#ProjectID").combobox('getValue');
                        var contractType = $("#ContractType").combobox('getValue');
                        var contractIDStr = $("#ContractID").combobox('getValues').toString();
                        var url = '/YearPlan/GetYearPlanDetailListForPreview?projectID=' + projectID + '&contractType=' + encodeURI(contractType) + '&contractIDStr=' + contractIDStr + '&statisticalCycle=';
                        initDetailTable(url);
                    }
                    $('#btnRevise').linkbutton('disable');
                    $('#btnDelete').linkbutton('disable');
                }
            }
        });
    }

    function initRevise(yearPlanID) {
        $("#YearPlanReviseID").combobox({
            editable: false, panelHeight: "auto",
            valueField: 'YearPlanReviseID',
            textField: 'ReviseDisplay',
            url: '/YearPlan/GetYearPlanReviseList?yearPlanID=' + yearPlanID,
            onLoadSuccess: function () {
                var data = $("#YearPlanReviseID").combobox('getData');
                if (data.length > 0) {
                    $("#YearPlanReviseID").combobox('select', data[0]["YearPlanReviseID"]);
                }
            },
            onSelect: function (record) {
                var url = '/YearPlan/GetYearPlanDetailList?projectID=' + $("#hProjectID").val() + '&contractType=' + encodeURI($("#ContractType").val()) + '&contractID=' + $("#hContractID").val() + '&yearPlanID=' + yearPlanID;
                if (record.YearPlanReviseID != '00000000-0000-0000-0000-000000000000') {
                    url = '/YearPlan/GetYearPlanReviseDetailList?projectID=' + $("#hProjectID").val() + '&contractType=' + encodeURI($("#ContractType").val()) + '&contractID=' + $("#hContractID").val() + '&yearPlanReviseID=' + record.YearPlanReviseID;
                }
                initDetailTable(url);
            }
        });
    }

    function initDetailTable(dataUrl) {
        $('#yearPlanDetailGrid').datagrid({
            url: dataUrl,
            iconCls: 'icon-bricks',
            nowrap: true,//当数据长度超出列宽时将会自动截取
            autoRowHeight: false,
            striped: true,//设置为true将交替显示行背景
            collapsible: true,
            pagination: true,
            pageSize: 20,
            remoteSort: false,
            idField: 'ID',//表明该列是一个唯一列。
            singleSelect: true,
            fitColumns: true,
            columns:
            [
                [
                    { title: '父项目名称', field: 'ParentStatisticsItemName', align: 'center', rowspan: 2, width: 100 },
                    { title: '项目名称', field: 'StatisticsItemName', align: 'center', rowspan: 2, width: 100 },
                    { title: '单位', field: 'Unit', align: 'center', rowspan: 2, width: 50 },
                    { title: '总工程量', field: '', width: 100 },
                    { title: '年度计划', field: '', colspan: 2, width: 140 },
                    { title: '1月份', field: '', colspan: 2, width: 160 },
                    { title: '2月份', field: '', colspan: 2, width: 160 },
                    { title: '3月份', field: '', colspan: 2, width: 160 },
                    { title: '4月份', field: '', colspan: 2, width: 160 },
                    { title: '5月份', field: '', colspan: 2, width: 160 },
                    { title: '6月份', field: '', colspan: 2, width: 160 },
                    { title: '7月份', field: '', colspan: 2, width: 160 },
                    { title: '8月份', field: '', colspan: 2, width: 160 },
                    { title: '9月份', field: '', colspan: 2, width: 160 },
                    { title: '10月份', field: '', colspan: 2, width: 160 },
                    { title: '11月份', field: '', colspan: 2, width: 160 },
                    { title: '12月份', field: '', colspan: 2, width: 160 }

                ],
                [
                     { title: '总金额', field: 'TotalAmount', align: 'center', width: 100 },
                     { title: '金额', field: 'YearPlanAmount', align: 'center', width: 80 },
                     { title: '占总金额%', field: 'YearPlanProportion', align: 'center', width: 60 },
                     { title: '金额', field: 'JanuaryAmount', align: 'center', width: 80 },
                     { title: '占年度计划%', field: 'JanuaryProportion', align: 'center', width: 80 },
                     { title: '金额', field: 'FebruaryAmount', align: 'center', width: 80 },
                     { title: '占年度计划%', field: 'FebruaryProportion', align: 'center', width: 80 },
                     { title: '金额', field: 'MarchAmount', align: 'center', width: 80 },
                     { title: '占年度计划%', field: 'MarchProportion', align: 'center', width: 80 },
                     { title: '金额', field: 'AprilAmount', align: 'center', width: 80 },
                     { title: '占年度计划%', field: 'AprilProportion', align: 'center', width: 80 },
                     { title: '金额', field: 'MayAmount', align: 'center', width: 80 },
                     { title: '占年度计划%', field: 'MayProportion', align: 'center', width: 80 },
                     { title: '金额', field: 'JuneAmount', align: 'center', width: 80 },
                     { title: '占年度计划%', field: 'JuneProportion', align: 'center', width: 80 },
                     { title: '金额', field: 'JulyAmount', align: 'center', width: 80 },
                     { title: '占年度计划%', field: 'JulyProportion', align: 'center', width: 80 },
                     { title: '金额', field: 'AugustAmount', align: 'center', width: 80 },
                     { title: '占年度计划%', field: 'AugustProportion', align: 'center', width: 80 },
                     { title: '金额', field: 'SeptemberAmount', align: 'center', width: 80 },
                     { title: '占年度计划%', field: 'SeptemberProportion', align: 'center', width: 80 },
                     { title: '金额', field: 'OctoberAmount', align: 'center', width: 80 },
                     { title: '占年度计划%', field: 'OctoberProportion', align: 'center', width: 80 },
                     { title: '金额', field: 'NovemberAmount', align: 'center', width: 80 },
                     { title: '占年度计划%', field: 'NovemberProportion', align: 'center', width: 80 },
                     { title: '金额', field: 'DecemberAmount', align: 'center', width: 80 },
                     { title: '占年度计划%', field: 'DecemberProportion', align: 'center', width: 80 }
                ]
            ],
            onLoadSuccess: function (data) {
                if (data.rows.length > 1) {
                    mergeCellsByField("yearPlanDetailGrid", "ParentStatisticsItemName,StatisticsItemName");
                }
                if (data.rows.length > 0) {
                    mergeColCells("yearPlanDetailGrid");
                }
            }
        });
    }

    var YearPlanList =
    {
        Add: function () {
            if ($("#hProjectID").val() == '' || $("#ContractType").val() == '' || $("#hContractID").val()=='') {
                $.messager.alert('操作提示', "项目、合同类型和标段不能为空！", 'info');
            }
            else {
                $.post("/UserMenuAction/GetUserDepartmentForFlow?menuCode=YearPlanList", function (userDepartment) {
                    if (userDepartment == "") {
                        showMyWindow("#myWindow", "选择机构", "/UserMenuAction/ShowOrgByUserForFlow?menuCode=YearPlanList", 500, 280, true, false);
                    }
                    else if (userDepartment == "**********") {
                        $.messager.alert('操作提示', '当前用户不属于任何机构，请先设置所属机构！', 'info');
                    }
                    else if (userDepartment == "++++++++++") {
                        $.messager.alert('操作提示', '当前用户的机构不属于流程申请配置机构，不能新增！', 'info');
                    }
                    else {
                        showMyWindow("#myWindow", "", '/YearPlan/YearPlan?MenuCode=YearPlan&sourceType=1&processType=&sourceID=&lastUpdateUserDepartment=' + userDepartment + '&projectID=' + $("#hProjectID").val() + '&contractType=' + encodeURI($("#ContractType").val()) + '&contractID=' + $("#hContractID").val(), 1000, 500, true, true, true);
                    }
                });
            }
        },

        Revise: function () {
            var row = $("#yearPlanGrid").datagrid("getSelected");
            var yearPlanReviseID = $("#YearPlanReviseID").combobox('getValue');
            if (yearPlanReviseID != '00000000-0000-0000-0000-000000000000') {
                $.post('/YearPlan/Revise?yearPlanID=' + row.ID + '&yearPlanReviseID=' + yearPlanReviseID, function (res) {
                    if (res == "Success") {
                        $('#btnRevise').linkbutton('disable');
                        $.messager.alert('操作提示', '修订成功!', 'info');
                    }
                    else {
                        if (res == "") {
                            $.messager.alert('操作提示', '删除失败!请查看该数据与其他模块下的信息的关联，或联系管理员。', 'info');
                        }
                        else {
                            $.messager.alert('操作提示', res, 'info');
                        }
                    }
                });
            }
            else {
                $.messager.alert('操作提示', '请先选择修订记录', 'info');
            }
        },

        Delete: function () {
            var row = $("#yearPlanGrid").datagrid("getSelected");
            $.messager.confirm('操作提示', "确认删除统计周期【" + row.StatisticalCycle + "】吗？", function (r) {
                if (r) {
                    $.post('/YearPlan/Delete?yearPlanID=' + row.ID, function (res) {
                        if (res == "Success") {
                            var url = '/YearPlan/GetYearPlanList?projectID=' + $("#hProjectID").val() + '&contractType=' + encodeURI($("#ContractType").val()) + '&contractID=' + $("#hContractID").val();
                            initTable(url);
                            $.messager.alert('操作提示', '删除成功!', 'info');
                        }
                        else {
                            if (res == "") {
                                $.messager.alert('操作提示', '删除失败!请查看该数据与其他模块下的信息的关联，或联系管理员。', 'info');
                            }
                            else {
                                $.messager.alert('操作提示', res, 'info');
                            }
                        }
                    });
                }
            });
        }
    }

    function cancelPopWindow(handleState) {
        if (handleState == "0") {
            var url = '/YearPlan/GetYearPlanList?projectID=' + $("#hProjectID").val() + '&contractType=' + encodeURI($("#ContractType").val()) + '&contractID=' + $("#hContractID").val();
            initTable(url);
        }
        colseMyWindow("#myWindow");
    }

    function CloseWindow(data) {
        colseMyWindow("#myWindow");
        var url = '/YearPlan/GetYearPlanList?projectID=' + $("#hProjectID").val() + '&contractType=' + encodeURI($("#ContractType").val()) + '&contractID=' + $("#hContractID").val();
        initTable(url);
        $.EasyuiSlide(data);
    }
</script>

<div class="easyui-layout" style="height:740px;width:2200px;">
    <input type="hidden" id="hProjectID" name="ProjectID" value="@Model.ProjectID" />
    <input type="hidden" id="hContractID" name="ContractID" value="@Model.ContractID" />
    <input type="hidden" id="hUserType" name="UserType" value="@Model.UserType" />
    <div data-options="region:'north'" style="height:35px;">
        <table style="margin-top: 5px">
            <tr id="trCommon">
                <td style="width: 95px; text-align: center">项目名称</td>
                <td>
                    <input id="ProjectID" name="ProjectID" class="easyui-combobox" style="width: 180px;" />
                </td>
                <td style="width: 95px; text-align: center">合同类型</td>
                <td>
                    <input id="ContractType" name="ContractType" class="easyui-combobox" style="width: 180px;" />
                </td>
                <td style="width: 95px; text-align: center">标段名称</td>
                <td colspan="3">
                    <input id="ContractID" name="ContractID" class="easyui-combobox" style="width: 450px;" />
                </td>
                <td>
                    <a href="#" id="btnSearch" class="easyui-linkbutton" data-options="plain:true" onclick="Search();">查看</a>
                </td>
            </tr>
            <tr id="trRequest">
                <td style="width: 95px; text-align: center">项目名称</td>
                <td>
                    <input name="ProjectName" id="ProjectName" class="easyui-textbox" data-options="required:true" value="@Model.ProjectName" style="width: 175px;" readonly />
                </td>
                <td style="width: 95px; text-align: center">合同类型</td>
                <td>
                    <input name="ContractType" id="ContractType" class="easyui-textbox" data-options="required:true" value="@Model.ContractType" style="width: 175px;" readonly />
                </td>
                <td style="width: 95px; text-align: center">标段名称</td>
                <td>
                    <input name="ContractName" id="ContractName" class="easyui-textbox" data-options="required:true" value="@Model.ContractName" style="width: 175px;" readonly />
                </td>
                <td style="width: 95px; text-align: center">修订记录</td>
                <td colspan="2">
                    <input id="YearPlanReviseID" name="YearPlanReviseID" class="easyui-combobox" style="width: 250px;" />
                </td>
            </tr>
        </table>
    </div>
    <div data-options="region:'center'">
        <div class="easyui-layout" fit="true">
            <div data-options="region:'north'" style="height:200px;">
                <table id="yearPlanGrid" fit="true"></table>
                <div id="yearPlanToolBar">
                    @Html.Raw(ViewBag.ToolBarInfo)
                </div>
            </div>
            <div data-options="region:'center'">
                <table id="yearPlanDetailGrid" fit="true"></table>
            </div>
        </div>
    </div>
</div>
